package com.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.exception.NoLoginException;

/**
 * 自定义拦截器
 * 1 implements HandlerInterceptor
 * 2 extends HandlerInterceptorAdapter
 * @author Administrator
 *
 */
public class SysInterceptor  implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("---------afterCompletion");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("---------postHandle");
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		System.out.println("---------------" + request.getRequestURI());
		// 设置不拦截url
		if (request.getRequestURI().equals("/smbms/") || request.getRequestURI().equals("/smbms/user/login")) {
			return true;
		} else if (request.getSession().getAttribute("userSession") == null) {
			// 重定向到指定页面
//			response.sendRedirect("/smbms");
//			return false;
			
			//异常
			throw new NoLoginException("用户未登录");
		}
		return true;
	}

}
